/**
 *
 * @Description:     类作用描述
 * @Author:          胡凯
 * @CreateDate:      2020-08-26
 * @Version:         1.0
 *
 */
import 'dart:convert';

import 'package:flutter/services.dart';
import 'package:o_amap/aMapModule/common/latlng.dart';
import 'package:o_amap/aMapModule/search/model/Input_tips_result.dart';
import 'package:o_amap/aMapModule/search/model/geocode_result.dart';
import 'model/poi_result.dart';
import 'model/poi_search_query.dart';
import 'model/regeocode_result.dart';

class AMapSearch {
  static AMapSearch _instance;

  static const _searchChannel = MethodChannel('com.p595263720.zit/search');

  AMapSearch._();

  factory AMapSearch() {
    if (_instance == null) {
      _instance = AMapSearch._();
      return _instance;
    } else {
      return _instance;
    }
  }

  /// 搜索poi
  Future<PoiResult> searchPoi(PoiSearchQuery query) {
    return _searchChannel
        .invokeMethod('searchPoi', {'query': query.toJsonString()})
        .then((result) => result as String)
        .then((jsonString) => PoiResult.fromJson(jsonDecode(jsonString)));
  }

  /// 搜索poi 周边搜索  周边搜索  PoiSearchQuery 中 SearchBound 属性Center Range 必填
  Future<PoiResult> searchPoiBound(PoiSearchQuery query) {
    return _searchChannel
        .invokeMethod('searchPoiBound', {'query': query.toJsonString()})
        .then((result) => result as String)
        .then((jsonString) => PoiResult.fromJson(jsonDecode(jsonString)));
  }

  /// 逆地理编码（坐标转地址）除了搜索外还会搜索出100米内所有信息地址信息（100米在原生端写死，如果需要拓展
  /// Author），可通过 ReGeocodeResult下regeocodeAddress下formatAddress拿到当前地址信息
  Future<ReGeocodeResult> searchReGeocode(LatLng point) {
    return _searchChannel
        .invokeMethod(
          'searchReGeocode',
          {'point': point.toJsonString()},
        )
        .then((result) => result as String)
        .then((jsonResult) => ReGeocodeResult.fromJson(jsonDecode(jsonResult)));
  }

  // input关键字搜索
  Future<InputTipsResult> inputTips(String keyword, {String city}) {
    return _searchChannel
        .invokeMethod(
          'inputTips',
          {'keyword': keyword, 'city': city},
        )
        .then((result) => result as String)
        .then((jsonResult) => InputTipsResult.fromJson(jsonDecode(jsonResult)));
  }

//
//  /// 搜索poi 多边形搜索
//  Future<PoiResult> searchPoiPolygon(PoiSearchQuery query) {
//    L.p('searchPoiPolygon dart端参数: query.toJsonString() -> ${query.toJsonString()}');
//
//    return _searchChannel
//        .invokeMethod(
//            'search#searchPoiPolygon', {'query': query.toJsonString()})
//        .then((result) => result as String)
//        .then((jsonString) => PoiResult.fromJson(jsonDecode(jsonString)));
//  }
//
//  /// 按id搜索poi
//  Future<PoiItem> searchPoiId(String id) {
//    L.p('searchPoiId dart端参数: id -> $id');
//
//    return _searchChannel
//        .invokeMethod('search#searchPoiId', {'id': id})
//        .then((result) => result as String)
//        .then((jsonString) => PoiItem.fromJson(jsonDecode(jsonString)));
//  }
//
//  /// 道路沿途直线检索POI
//  Future<RoutePoiResult> searchRoutePoiLine(RoutePoiSearchQuery query) {
//    L.p('searchRoutePoiLine dart端参数: query.toJsonString() -> ${query.toJsonString()}');
//
//    return _searchChannel
//        .invokeMethod(
//            'search#searchRoutePoiLine', {'query': query.toJsonString()})
//        .then((result) => result as String)
//        .then((jsonString) => RoutePoiResult.fromJson(jsonDecode(jsonString)));
//  }
//
//  /// 道路沿途多边形检索POI
//  Future<RoutePoiResult> searchRoutePoiPolygon(RoutePoiSearchQuery query) {
//    L.p('searchRoutePoiPolygon dart端参数: query.toJsonString() -> ${query.toJsonString()}');
//
//    return _searchChannel
//        .invokeMethod(
//            'search#searchRoutePoiPolygon', {'query': query.toJsonString()})
//        .then((result) => result as String)
//        .then((jsonString) => RoutePoiResult.fromJson(jsonDecode(jsonString)));
//  }
//
//  /// 计算驾驶路线
//  Future<DriveRouteResult> calculateDriveRoute(RoutePlanParam param) {
//    final _routePlanParam = param.toJsonString();
//    L.p('方法calculateDriveRoute dart端参数: _routePlanParam -> $_routePlanParam');
//    return _searchChannel
//        .invokeMethod(
//          'search#calculateDriveRoute',
//          {'routePlanParam': _routePlanParam},
//        )
//        .then((result) => result as String)
//        .then(
//            (jsonResult) => DriveRouteResult.fromJson(jsonDecode(jsonResult)));
//  }
//
//  /// 地址转坐标 [name]表示地址，第二个参数表示查询城市，中文或者中文全拼，citycode、adcode
  Future<GeocodeResult> searchGeocode({String name, String city}) {
    return _searchChannel
        .invokeMethod(
          'searchGeocode',
          {'name': name, 'city': city},
        )
        .then((result) => result as String)
        .then((jsonResult) => GeocodeResult.fromJson(jsonDecode(jsonResult)));
  }
//
//  /// 距离测量 参考[链接](https://lbs.amap.com/api/android-sdk/guide/computing-equipment/distancesearch)
//  ///
//  /// type 分别对应
//  Future<List<int>> distanceSearch(
//      List<LatLng> origins, LatLng target, DistanceSearchType type) async {
//    List<Map<String, Object>> oriList = [];
//
//    origins.forEach((o) {
//      oriList.add(o.toJson());
//    });
//
//    Map<String, dynamic> params = {
//      "origin": oriList,
//      "target": target.toJson(),
//      "type": DistanceSearchType.values.indexOf(type),
//    };
//
//    List<dynamic> result =
//        await _searchChannel.invokeMethod("tool#distanceSearch", params);
//    return result.map((v) => v as int).toList();
//  }
//
//  /// 公交站点查询
//  ///
//  /// [stationName] 公交站点名
//  /// [city] 所在城市名或者城市区号
//  Future<BusStationResult> searchBusStation(String stationName, String city) {
//    L.p('方法searchBusStation dart端参数: stationName -> $stationName, city -> $city');
//
//    return _searchChannel
//        .invokeMethod(
//          'search#searchBusStation',
//          {'stationName': stationName, 'city': city},
//        )
//        .then((result) => result as String)
//        .then((json) {
//          if (Platform.isIOS) {
//            return BusStationResult.ios(
//                BusStationResult_iOS.fromJson(jsonDecode(json)));
//          } else if (Platform.isAndroid) {
//            return BusStationResult.fromJson(jsonDecode(json));
//          } else {
//            return null;
//          }
//        });
//  }
}

enum DistanceSearchType {
  line,
  driver,
}
